package com.timkj.zgyd.controller;

import com.github.wxpay.sdk.WXPayUtil;
import com.timkj.zgyd.dto.CreateOrderDto;
import com.timkj.zgyd.entity.ResultVO;
import com.timkj.zgyd.service.ZgydOrderService;
import com.timkj.zgyd.wxPay.PayConfig;
import com.timkj.zgyd.wxPay.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : liji
 * @date : 2020-09-01 15:13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/zgyd/order")
@Api(value = "订单接口",tags = {"订单接口"})
public class ZgydOrderController {
    private WxPayService wxPayService;
    private ZgydOrderService zgydOrderService;

    @Autowired
    public ZgydOrderController(WxPayService wxPayService, ZgydOrderService zgydOrderService) {
        this.wxPayService = wxPayService;
        this.zgydOrderService = zgydOrderService;
    }

    @ApiOperation(value = "创建订单接口")
    @RequestMapping(value = "/createOrder",method = RequestMethod.POST)
    public ResultVO createOrder(@RequestBody @Valid CreateOrderDto dto,HttpServletRequest request){
        return zgydOrderService.createOrder(dto,request);
    }

    @ApiOperation(value = "查看订单剩余时间")
    @RequestMapping(value = "/searchOrder",method = RequestMethod.POST)
    public ResultVO searchOrder(@RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                                @RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                                HttpServletRequest request){
        return zgydOrderService.searchOrder(xtddh,openid,request);
    }

    @ApiOperation(value = "判断当前用户是否有订单")
    @RequestMapping(value = "/isOrder",method = RequestMethod.GET)
    public ResultVO isOrder(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                            HttpServletRequest request){
        return zgydOrderService.isOrder(openid,request);
    }

    @ApiOperation(value = "取消订单按钮")
    @RequestMapping(value = "/deleteOrder",method = RequestMethod.GET)
    public ResultVO deleteOrder(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                                @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                                HttpServletRequest request){
        return zgydOrderService.deleteOrder(openid,xtddh,request);
    }

    @ApiOperation(value = "查询用户是否填写个人信息")
    @RequestMapping(value = "/submitForm",method = RequestMethod.GET)
    public ResultVO isSubmitForm(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                               @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                               HttpServletRequest request){
        return zgydOrderService.isSubmitForm(openid,xtddh,request);
    }

    @ApiOperation(value = "表单提交")
    @RequestMapping(value = "/submitForm",method = RequestMethod.POST)
    public ResultVO submitForm(@RequestParam("name") @Valid @NotBlank(message = "name不能为空") String name,
                               @RequestParam("idCard") @Valid @NotBlank(message = "idCard不能为空") String idCard,
                               @RequestParam("college") @Valid @NotBlank(message = "college不能为空") String college,
                               @RequestParam("grade") @Valid @NotBlank(message = "grade不能为空") String grade,
                               @RequestParam("lxfs") @Valid @NotBlank(message = "lxfs不能为空") String lxfs,
                               @RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                               @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                               HttpServletRequest request){
        return zgydOrderService.submitForm(name,idCard,college,grade,lxfs,openid,xtddh,request);
    }

    /**
     * 修改状态正在支付
     * 调用统一下单,并获取支付跳转链接
     * @param total_fee	支付金额
     * @param ip 客户ip
     * @return
     */
    @ApiOperation(value = "统一下单")
    @RequestMapping(value = "/wxPay",method = RequestMethod.POST)
    public Map<String,String> wxPay(@RequestParam("total_fee") @Valid @NotBlank(message = "total_fee不能为空") String total_fee,
                                    @RequestParam("ip") @Valid @NotBlank(message = "ip不能为空") String ip,
                                    @RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                                    @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                                    HttpServletRequest request){
        return wxPayService.payH5(total_fee,ip,openid,xtddh,request);
    }

    @ApiOperation(value = "H5拉起微信支付")
    @RequestMapping(value = "/wxPayH5",method = RequestMethod.POST)
    public Map<String,String> wxPayH5(@RequestParam("prepay_id") String prepay_id){
        Map<String,String> hashMap = new HashMap<>();
        hashMap.put("appId","wxc66c054a1c53bc0d");
        hashMap.put("timeStamp", String.valueOf(System.currentTimeMillis()));
        hashMap.put("nonceStr",WXPayUtil.generateNonceStr());
        hashMap.put("package","prepay_id=" + prepay_id);
        hashMap.put("signType","MD5");
        try {
            hashMap.put("paySign",WXPayUtil.generateSignature(hashMap,PayConfig.API_KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hashMap;
    }

    @ApiOperation(value = "微信支付通知")
    @RequestMapping(value = "/getWxInform",method = RequestMethod.POST)
    public String getWxInform(HttpServletRequest request){
        log.info("微信支付回调通知");
        return zgydOrderService.getWxInform(request);
    }

    @ApiOperation(value = "修改订单状态")
    @RequestMapping(value = "/changeOrderStatus",method = RequestMethod.POST)
    public ResultVO submitForm(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                               @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                               @RequestParam("status") @Valid @NotBlank(message = "status不能为空") String status,
                               HttpServletRequest request){
        return zgydOrderService.changeOrderStatus(openid,xtddh,status,request);
    }

    @ApiOperation(value = "发送模板消息")
    @RequestMapping(value = "/sendMessage",method = RequestMethod.POST)
    public ResultVO sendMessage(@RequestParam("openid") @Valid @NotBlank(message = "openid不能为空") String openid,
                                @RequestParam("xtddh") @Valid @NotBlank(message = "xtddh不能为空") String xtddh,
                                HttpServletRequest request){
        return zgydOrderService.sendMessage(openid,xtddh,request);
    }
}
